<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/3 0003
 * Time: 18:57
 */

namespace app\admin\controller;


use app\common\RedisCache;
use think\Controller;
use think\Db;
use think\Request;

class Base extends Controller
{
    public $admin;
    public $table;
    public $site_id;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->admin = session('admin');
        if (empty($this->admin)) {
            $this->redirect(url('login/index'));
        }
        //权限验证
//        if (!$this->auth()) {
//            $this->error('无权限操作');
//        }
        $this->site_id = $this->admin['site_id'];
        $site = Db::name('site')->where('id',$this->site_id)->find();
        if(!$site){
            session('admin',null);
            $this->error('站点不存在');
        }
        $this->assign('site',$site);
    }

    private function auth()
    {
        $controller = request()->controller();
        $action = request()->action();
        $config = config('rbac');

        //ajax无需检测
        if (substr($action, 0, 4) === 'pass') {
            return true;
        }

        //无需检测的控制器和方法
        $no_auth_controller = $config['no_auth_controller'] ? explode(',', $config['no_auth_controller']) : [];
        $no_auth_action = $config['no_auth_action'] ? explode(',', $config['no_auth_action']) : [];
        if (in_array($controller, $no_auth_controller)) {
            return true;
        }
        if (in_array($action, $no_auth_action)) {
            return true;
        }

        //实时检测
        $admin = Db::name('admin')
            ->where('id', $this->admin['id'])
            ->find();
        //超管无需检测
        if ($admin['is_super']) {
            return true;
        }
        if (!$admin['status']) {
            session('admin', null);
            session('nav', null);
        }
        if ($admin['role_id']) {
            $access = Db::name('rbac_access')
                ->alias('a')
                ->join('rbac_node b', 'a.node_id=b.id')
                ->where('a.role_id', $admin['role_id'])
                ->where('b.controller', $controller)
                ->where('b.action', $action)
                ->find();
            if ($access) {
                return true;
            }
        }
        return false;
    }

    /**
     * 删除
     */
    public function ajax_del()
    {
        $id = input('id');
        $a = Db::name($this->table)->where('id', $id)->delete();
        if ($a) {
            echo api_return(1, '删除成功');
        } else {
            echo api_return(0, '删除失败');
        }
    }

    /**
     * 密码强度
     */
    public function checkPasswordStrength($password)
    {
        $reg = '/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-\.]).{8,24}$/';
        if(!preg_match($reg,$password)){
            return false;
        }
        return true;
    }

    /**
     * 清空所有redis缓存
     */
    public function ajax_empty_redis()
    {
        $res = RedisCache::getInstance()->empty_redis();
        if($res){
            return dataReturn(1,'操作成功');
        }
        return dataReturn(0,'操作失败');
    }
}